<?php

namespace App\Utility;

use EasySwoole\Utility\Time;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class ExcelClient{


    public function download($lists){
        try{
            $spreadsheet = new Spreadsheet();
            $sheet       = $spreadsheet->getActiveSheet();
            $styleArray = [
                'alignment' => [
                    'horizontal' => Alignment::HORIZONTAL_CENTER,
                ],
            ];

            //设置第一行的表头并设置每行水平居中
            $sheet->setCellValue('A1', '日期')->getStyle('A1')->applyFromArray($styleArray);
            $sheet->setCellValue('B1', '订单编号')->getStyle('B1')->applyFromArray($styleArray);
            $sheet->setCellValue('C1', '标题')->getStyle('C1')->applyFromArray($styleArray);
            $sheet->setCellValue('D1', '联系人')->getStyle('D1')->applyFromArray($styleArray);
            $sheet->setCellValue('E1', '联系人电话')->getStyle('E1')->applyFromArray($styleArray);
            $sheet->setCellValue('F1', '部门')->getStyle('F1')->applyFromArray($styleArray);
            $sheet->setCellValue('G1', '详细地址')->getStyle('G1')->applyFromArray($styleArray);
            $sheet->setCellValue('H1', '维修人员')->getStyle('H1')->applyFromArray($styleArray);
            $sheet->setCellValue('I1', '维修人员电话')->getStyle('I1')->applyFromArray($styleArray);
            $sheet->setCellValue('J1', '实际费用')->getStyle('J1')->applyFromArray($styleArray);
            $sheet->setCellValue('K1', '订单状态')->getStyle('K1')->applyFromArray($styleArray);
            // 设置个表格宽度
            $spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(16);
            $spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(20);
            $spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(16);
            $spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(16);
            $spreadsheet->getActiveSheet()->getColumnDimension('E')->setWidth(16);
            $spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(20);
            $spreadsheet->getActiveSheet()->getColumnDimension('G')->setWidth(36);
            $spreadsheet->getActiveSheet()->getColumnDimension('H')->setWidth(16);
            $spreadsheet->getActiveSheet()->getColumnDimension('I')->setWidth(16);
            $spreadsheet->getActiveSheet()->getColumnDimension('J')->setWidth(20);
            $spreadsheet->getActiveSheet()->getColumnDimension('K')->setWidth(16);
            // 垂直居中
            $spreadsheet->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('C')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('E')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('F')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('G')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('H')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('I')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('J')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('K')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            //把数据循环写入到excel里
            $key = 2;
            foreach ($lists as $k => $v) {
                //从第二行开始写
                $sheet->setCellValue('A' . $key, date('Y-m-d',Time::endTimestamp($v['created_at'])))->getStyle('A' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('B' . $key, $v['order_id'])->getStyle('B' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('C' . $key, $v['title'])->getStyle('C' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('D' . $key, $v['user_plain_name'])->getStyle('D' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('E' . $key, $v['user_plain_mobile'])->getStyle('E' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('F' . $key, $v['job'])->getStyle('F' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('G' . $key, $v['user_plain_address'])->getStyle('G' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('H' . $key, $v['user_repair_name'])->getStyle('H' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('I' . $key, $v['user_repair_mobile'])->getStyle('I' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('J' . $key, $v['money'])->getStyle('J' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('K' . $key, $v['status_text'])->getStyle('K' . $key)->applyFromArray($styleArray);
                $key++;
            }
            $writer   = new Xlsx($spreadsheet);
            //这里可以写绝对路径，其他框架到这步就结束了
            $name = date('Y-m-d',time()).'.xls';
            $writer->save($name);
            //关闭连接，销毁变量
            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);
            return $name;
        }catch (\Throwable $e){
            throw new \Exception($e->getMessage());
        }
    }

    public function download1($AResult){
        try{
            $spreadsheet = new Spreadsheet();
            $sheet       = $spreadsheet->getActiveSheet();
            $styleArray = [
                'alignment' => [
                    'horizontal' => Alignment::HORIZONTAL_CENTER,
                ],
            ];
            //设置第一行的表头并设置每行水平居中
            $sheet->setCellValue('A1', 'id编号')->getStyle('A1')->applyFromArray($styleArray);
            $sheet->setCellValue('B1', '图片')->getStyle('B1')->applyFromArray($styleArray);
            $sheet->setCellValue('C1', '图片1')->getStyle('C1')->applyFromArray($styleArray);
            $sheet->setCellValue('D1', '图片2')->getStyle('D1')->applyFromArray($styleArray);
            // 设置个表格宽度
            $spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(16);
            $spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(20);
            $spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(16);
            $spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(16);
            // 垂直居中
            $spreadsheet->getActiveSheet()->getStyle('A')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('B')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('C')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            $spreadsheet->getActiveSheet()->getStyle('D')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
            //把数据循环写入到excel里
            $key = 2;
            foreach ($AResult as $k => $v) {
                //从第二行开始写
                $sheet->setCellValue('A' . $key, $v)->getStyle('A' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('C' . $key, $v)->getStyle('C' . $key)->applyFromArray($styleArray);
                $sheet->setCellValue('D' . $key, $v)->getStyle('D' . $key)->applyFromArray($styleArray);
                //到处图片代码
                $objDrawing = new Drawing();
                // 图片生成
                $objDrawing->setName('Logo');
                $objDrawing->setDescription('Logo');
                $objDrawing->setPath(EASYSWOOLE_ROOT .'/Public/upload/2019-04-11/7360eccc5fe52a73cd0621ea3645f3fd.jpg');
                //设置宽度高度
//                $objDrawing->setHeight(50);//照片高度
                $objDrawing->setWidth(100); //照片宽度
                $objDrawing->setCoordinates('B' . $key);
                // 图片偏移距离
                $objDrawing->setOffsetX(20);
                $objDrawing->setOffsetY(20);
                $objDrawing->setWorksheet($spreadsheet->getActiveSheet());
                $sheet->getRowDimension($key)->setRowHeight(130);
                $key++;
            }
            $writer   = new Xlsx($spreadsheet);
            //这里可以写绝对路径，其他框架到这步就结束了
            $name = 'test.xls';
            $writer->save($name);
            //关闭连接，销毁变量
            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);
            return $name;
        }catch (\Throwable $e){
            throw new \Exception($e->getMessage());
        }
    }
}